﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DataAccess_Layer;
using Business_Layer;
using System.Collections;
public partial class MyAdmin_QL_User_AM : System.Web.UI.Page
{
    LogAction log = new LogAction();
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            refreshGridView();
        }        
    }
    private void refreshGridView()
    {
        gvUsers.DataSource = DataAccess_Layer.Aspnet_MembershipDAC.getQLUser();
        gvUsers.DataBind();
    }
    protected void usersGRIDVIEW_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void usersGRIDVIEW_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

    }
    protected void usersGRIDVIEW_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvUsers.EditIndex = e.NewEditIndex;
        refreshGridView();
        refreshCBB(e.NewEditIndex);
    }
    private void refreshCBB(int rowIndex)
    {
        DropDownList cbbRoleForEdit = (DropDownList)gvUsers.Rows[rowIndex].FindControl("cbbRoleForEdit");
        Label lblRoleNameForEdit = (Label)gvUsers.Rows[rowIndex].FindControl("lblRoleNameForEdit");
        string currentRoleName = lblRoleNameForEdit.Text;
        cbbRoleForEdit.SelectedValue = currentRoleName;
    }
    protected void usersGRIDVIEW_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    }
    protected void usersGRIDVIEW_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvUsers.EditIndex = -1;
        refreshGridView();
    }
    protected void gvUsers_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList cbbRole = (DropDownList)gvUsers.Rows[e.RowIndex].FindControl("cbbRoleForEdit");
        string userName = ((Label)gvUsers.Rows[e.RowIndex].FindControl("lblUserName")).Text;
        string newUserRoleName = cbbRole.SelectedItem.ToString();
        string oldUserRoleName = DataAccess_Layer.UserInRoleDAC.timRoleNameCuaUser(userName);
        CheckBox checkBanned = (CheckBox)gvUsers.Rows[e.RowIndex].Cells[2].Controls[0];
        
        Roles.RemoveUserFromRole(userName, oldUserRoleName);
        Roles.AddUserToRole(userName, newUserRoleName);

        DataAccess_Layer.Aspnet_MembershipDAC.setIsLockedOut(userName, checkBanned.Checked);
        
        gvUsers.EditIndex = -1;
        refreshGridView();
    }
    protected void cbbRoleForEdit_DataBound(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList cbbRole = (DropDownList)sender;
            Label lblRoleNameForEdit = (Label)cbbRole.Parent.FindControl("lblRoleNameForEdit");
            string currentRoleName = lblRoleNameForEdit.Text;
            cbbRole.SelectedValue = currentRoleName;
        }        
    }
    protected void cbbRoleForEdit_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
    protected void btnXoaNhieu_Click(object sender, EventArgs e)
    {
        int lastCellIndex = gvUsers.Rows[0].Cells.Count - 1;
        foreach (GridViewRow r in gvUsers.Rows)
        {
            CheckBox checkXoa = (CheckBox)r.Cells[lastCellIndex].FindControl("checkXoa");
            if (checkXoa.Checked)
            {
                string userName = ((Label)r.Cells[1].FindControl("lblUserName")).Text;
                //log
                log.TenLoaiCapNhat = "Xóa";
                log.Ngay = DateTime.Now;
                log.TenBang = "Bảng quản lý user admin";
                log.TenLoaiDangNhap = "Admin";
                log.TenDangNhap = Helper.getCurrentAdminUserName(Session);
                string logText = "";
                log.DuLieuCu = "Username:" + userName;
                logText = "X";
                log.DuLieuMoi = logText;
                Log_BUS.insert(log);
                //log
                Membership.DeleteUser(userName, true);
            }
        }

        refreshGridView();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        Response.Redirect("TaoTKHeAdmin.aspx");
    }
}